package com.zxr.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zxr.common.annotation.Log;
import com.zxr.common.enums.BusinessType;
import com.zxr.system.domain.Products;
import com.zxr.system.service.IProductsService;
import com.zxr.common.core.controller.BaseController;
import com.zxr.common.core.domain.AjaxResult;
import com.zxr.common.utils.poi.ExcelUtil;
import com.zxr.common.core.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author zxrproject
 * @date 2025-04-26
 */
@Controller
@RequestMapping("/system/products")
public class ProductsController extends BaseController
{
    private String prefix = "system/products";

    @Autowired
    private IProductsService productsService;

    @RequiresPermissions("system:products:view")
    @GetMapping()
    public String products()
    {
        return prefix + "/products";
    }

    /**
     * 查询商品列表
     */
    @RequiresPermissions("system:products:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Products products)
    {
        startPage();
        List<Products> list = productsService.selectProductsList(products);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("system:products:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Products products)
    {
        List<Products> list = productsService.selectProductsList(products);
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("system:products:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("system:products:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Products products)
    {
        return toAjax(productsService.insertProducts(products));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("system:products:edit")
    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId") Long productId, ModelMap mmap)
    {
        Products products = productsService.selectProductsByProductId(productId);
        mmap.put("products", products);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("system:products:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Products products)
    {
        return toAjax(productsService.updateProducts(products));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("system:products:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Long ids)
    {
        return toAjax(productsService.deleteProductsByProductId(ids));
    }

    /**
     * 上架
     */
    @RequiresPermissions("system:products:productList")
    @Log(title = "商品", businessType = BusinessType.OTHER)
    @PostMapping( "/productList/{id}")
    @ResponseBody
    public AjaxResult productList(@PathVariable("id") Long id)
    {
        Products products = new Products();
        products.setStatus(1);
        products.setProductId(id);
        return toAjax(productsService.updateProducts(products));
    }

    /**
     * 下架
     */
    @RequiresPermissions("system:products:productDelist")
    @Log(title = "商品", businessType = BusinessType.OTHER)
    @PostMapping( "/productDelist/{id}")
    @ResponseBody
    public AjaxResult productDelist(@PathVariable("id") Long id)
    {
        Products products = new Products();
        products.setStatus(2);
        products.setProductId(id);
        return toAjax(productsService.updateProducts(products));
    }
}
